//
//  TBCSettingViewController.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCSettingViewController.h"
#import "BaseTableView.h"
#import "BaseTableViewCell.h"
#import "TBCPermissionViewController.h"
#import "TBCAboutOurViewController.h"
#import <UIImageView+WebCache.h>
@interface TBCSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)BaseTableView *tableView;
@property(nonatomic,strong)UIImageView *imageView;

@property(nonatomic,strong)UISwitch *mySwitch;

@end

@implementation TBCSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[BaseTableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    self.tableView.contentInset = UIEdgeInsetsMake(200.0*HEIGHTSUIT+20, 0, 0, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200.0*HEIGHTSUIT, self.view.frame.size.width, 200.0*HEIGHTSUIT)];
    self.imageView.image = [UIImage imageNamed:@"hua.jpg"];
    //设置imageView高度改变时宽度也跟着改变
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.tableView addSubview:self.imageView];

    
    
    
    self.mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(310*WIDTHSUIT, 7, 50, 40*HEIGHTSUIT)];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = @"开放源代码许可协议";
    }
    if (indexPath.section == 1) {
        cell.textLabel.text = @"清除缓存";
    }
    if (indexPath.section == 2) {
        cell.textLabel.text = @"夜间模式";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.mySwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"night"];
        [self.mySwitch addTarget:self action:@selector(mySwitchChange:) forControlEvents:UIControlEventValueChanged];
        [cell addSubview:self.mySwitch];
    }
    if (indexPath.section == 3) {
        cell.textLabel.text = @"关于我们";
    }
    
    return cell;
}


-(void)mySwitchChange:(UISwitch *)mySwitch
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"night"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:@"no"];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"night"];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:@"yes"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"night"];
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        TBCPermissionViewController *permissionVC = [[TBCPermissionViewController alloc] init];
        [self.navigationController pushViewController:permissionVC animated:YES];
    }
    if (indexPath.section == 1) {
        NSInteger size = [[SDImageCache sharedImageCache] getSize];
        float totalSize = size / 1024.0 / 1024.0;
        NSLog(@"totalSize = %f",totalSize);
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清除缓存" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alertC addAction:cancelAction];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSInteger size = [[SDImageCache sharedImageCache] getSize];
            float totalSize = size / 1024.0 / 1024.0;
            NSLog(@"totalSize = %f",totalSize);
            
            NSString *str = [NSString stringWithFormat:@"已清除缓存%.2fM",totalSize];
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
            [[SDImageCache sharedImageCache] clearDisk];
            [self presentViewController:alertController animated:YES completion:nil];
            [self dismissViewControllerAnimated:YES completion:nil];
            
            
        }];
        [alertC addAction:okAction];
        
        [self presentViewController:alertC animated:YES completion:nil];
    }
    if (indexPath.section == 3) {
        TBCAboutOurViewController *aboutVC = [[TBCAboutOurViewController alloc] init];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y + 64;//加上导航栏高度,第一次是-200
    NSLog(@"y2 === %f",y);
    
    if (y < -200.0*HEIGHTSUIT) {
        CGRect frame = self.imageView.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
        self.imageView.frame = frame;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
